package com.aaa.service.impl;

import com.aaa.entity.Dept;
import com.aaa.dao.DeptDao;
import com.aaa.service.DeptService;
import com.aaa.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 部门表(Dept)表服务实现类
 *
 * @author makejava
 * @since 2022-11-17 14:38:40
 */
@Service("deptService")
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptDao deptDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result<Dept> queryById(Integer id) {
        Dept dept = this.deptDao.queryById(id);
        return Result.success(dept,"查询成功");
    }

    /**
     * 分页查询
     *
     * @param dept        筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Result<Page<Dept>> queryByPage(Dept dept, PageRequest pageRequest) {
        long total = this.deptDao.count(dept);
        PageImpl<Dept> deptPage = new PageImpl<>(this.deptDao.queryAllByLimit(dept, pageRequest), pageRequest, total);
        return Result.success(deptPage,"分页查询成功");
    }

    /**
     * 新增数据
     *
     * @param dept 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Dept> insert(Dept dept) {
        int i = this.deptDao.insert(dept);
        if(i == 0){
            return Result.error("添加部门失败");
        }
        return Result.success(dept,"添加部门成功");
    }

    /**
     * 修改数据
     *
     * @param dept 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Dept> update(Dept dept) {
        int i = this.deptDao.update(dept);
        Dept afterDept = deptDao.queryById(dept.getId());
        if(i == 0){
            return Result.error("修改成功");
        }
        return Result.success(afterDept,"修改成功");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result<Boolean> deleteById(Integer id) {
        int i = this.deptDao.deleteById(id);
        if(i == 0){
            return Result.error(false,"删除失败");
        }
        return Result.success(true,"删除失败");
    }
}
